package cn.iocoder.yudao.module.dify.service.fileprocessing;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.dify.api.vo.resp.StreamResponse;
import cn.iocoder.yudao.module.dify.controller.admin.fileprocessing.vo.dto.DifyFileDTO;
import cn.iocoder.yudao.module.dify.service.DifyService;
import cn.iocoder.yudao.module.dify.util.DifyFileUploadUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;
import cn.iocoder.yudao.module.dify.controller.admin.fileprocessing.vo.*;
import cn.iocoder.yudao.module.dify.dal.dataobject.fileprocessing.FileProcessingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dify.dal.mysql.fileprocessing.FileProcessingMapper;
import reactor.core.publisher.Flux;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.dify.enums.ErrorCodeConstants.*;

/**
 * Dify文件处理跟踪 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FileProcessingServiceImpl implements FileProcessingService {

    @Resource
    private FileProcessingMapper fileProcessingMapper;


    @Resource
    private DifyService difyService;

    @Override
    @Transactional
    public Long createFileProcessing(FileProcessingSaveReqVO createReqVO) throws IOException {
        // 插入
        FileProcessingDO fileProcessing = BeanUtils.toBean(createReqVO, FileProcessingDO.class);
        fileProcessing.setUserId(getLoginUserId());
        fileProcessingMapper.insert(fileProcessing);
        String uploadFileByDify = difyService.uploadFileByDify(fileProcessing);
        DifyFileUploadUtil.validate(uploadFileByDify);
        Assert.notNull(uploadFileByDify,"dify上传文件响应为空");
        DifyFileDTO difyFileDTO = JSONUtil.toBean(uploadFileByDify, DifyFileDTO.class);
        fileProcessing.setDifyFileId(difyFileDTO.getId());
        fileProcessing.setCreateByUid(difyFileDTO.getCreatedBy());
        fileProcessingMapper.updateById(fileProcessing);
        // 返回
        return fileProcessing.getId();
    }

    @Override
    public void updateFileProcessing(FileProcessingSaveReqVO updateReqVO) {
        // 校验存在
        validateFileProcessingExists(updateReqVO.getId());
        // 更新
        FileProcessingDO updateObj = BeanUtils.toBean(updateReqVO, FileProcessingDO.class);
        fileProcessingMapper.updateById(updateObj);
    }

    @Override
    public void deleteFileProcessing(Long id) {
        // 校验存在
        validateFileProcessingExists(id);
        // 删除
        fileProcessingMapper.deleteById(id);
    }

    private void validateFileProcessingExists(Long id) {
        if (fileProcessingMapper.selectById(id) == null) {
            throw exception(FILE_PROCESSING_NOT_EXISTS);
        }
    }

    @Override
    public FileProcessingDO getFileProcessing(Long id) {
        return fileProcessingMapper.selectById(id);
    }

    @Override
    public PageResult<FileProcessingDO> getFileProcessingPage(FileProcessingPageReqVO pageReqVO) {
        return fileProcessingMapper.selectPage(pageReqVO);
    }

    /**
     * 创建风险管理评估报告
     * @param id
     * @return
     */
    public void workFolowRistManagerReport(Long id) throws IOException {
        FileProcessingDO fileProcessingDO = fileProcessingMapper.selectById(id);
        Assert.notNull(fileProcessingDO,"对应内容不存在，请检查");

        //开始调用
        difyService.uploadFileByDify(fileProcessingDO);
    }

    @Override
    public List<FileProcessingDO> getSimpFileleList(Long userId) {
     return fileProcessingMapper.selectListByUserId(userId);
    }


    @Override
    public List<FileProcessingDO> getFileMsgByFileId(List<String> fileIdList) {
        return fileProcessingMapper.selectListByDifyFileId(fileIdList);
    }

}